(function(angular) {
	'use strict';
	angular.module('fs.waf').controller('cda.lookup.reportCompanyController', reportCompanyController);
	reportCompanyController.$inject = [ '$scope', '$q', '$uibModalInstance', 'Constants', 'dialogsModule', 'gridService', 'modalService', 'layoutConfig', 'validatorConfig', 'authService', '$filter' ];
	function reportCompanyController($scope, $q, $uibModalInstance, Constants, dialogsModule, gridService, modalService, layoutConfig, validatorConfig, authService, $filter) {
		var ctrl = this, log = $scope.loggerFactory.getLogger("reportCompanyLookup");
		ctrl.dialogsModule = dialogsModule;
		ctrl.layoutConfig = layoutConfig;
		ctrl.reportFormatsOptions = ctrl.dialogsModule.params.reportFormatsOptions;
		ctrl.reportCompany = {};
		ctrl.ok = function() {
			if (!dialogsModule.btnCallback || !dialogsModule.btnCallback['ok'] || dialogsModule.btnCallback['ok'](ctrl) !== false) {
				$uibModalInstance.close();
			}
		};
		
		ctrl.close = function() {
			if (!dialogsModule.btnCallback || !dialogsModule.btnCallback['close'] || dialogsModule.btnCallback['close']() !== false) {
				$uibModalInstance.dismiss();
			}
		};
		
		ctrl.init = function() {
			ctrl.gridOptions = gridService.createGridOptions($scope, ctrl.layoutConfig.tableConfigs.table, paginationChanged, rowSelectionChanged, sortChanged);
			ctrl.gridOptions.data = 'vm.searchCriteria.list';
			ctrl.searchCriteria = ctrl.gridOptions.defaultSearchCriteria;
			ctrl.gridOptions.multiSelect = true;
			
			if(ctrl.dialogsModule.params.reportFormatList){
				ctrl.reportCompany.reportFormatList = [];
				angular.forEach(ctrl.dialogsModule.params.reportFormatList, function(format, key) {
					ctrl.reportCompany.reportFormatList.push(format);
				});
			}
			if (ctrl.dialogsModule.params.reportCompanyCodes) {
				ctrl.searchCriteria.filterCompanyCodes = $filter('encode')(ctrl.dialogsModule.params.reportCompanyCodes);// get params from parent page
			}
			if(ctrl.dialogsModule.params && "ALL" != $filter('encode')(ctrl.dialogsModule.params.companyCode)){
				ctrl.searchCriteria.partyCode = $filter('encode')(ctrl.dialogsModule.params.partyCode);
			}
			ctrl.searchCriteria.isReportCompany = true;
			if(ctrl.dialogsModule.params && ctrl.dialogsModule.params.companyTypeList) {
				ctrl.searchCriteria.companyTypeList = ctrl.dialogsModule.params.companyTypeList;
			}
			if(ctrl.dialogsModule.params && ctrl.dialogsModule.params.serviceCode) {
				ctrl.searchCriteria.serviceCode = $filter('encode')(ctrl.dialogsModule.params.serviceCode);
			}
			ctrl.search();
		};

		ctrl.search = function() {
			ctrl.searchCriteria.list = [];
			authService.search(ctrl.searchCriteria).then(function(response) {
				angular.merge(ctrl.searchCriteria, response.data);
				gridService.updateGridOptions(ctrl.searchCriteria, ctrl.gridOptions);
			}, angular.noop);
		};

		function paginationChanged(pageNum, pageSize) {
			ctrl.searchCriteria.pageNum = pageNum;
			ctrl.searchCriteria.pageSize = pageSize;
			ctrl.searchCriteria.list = [];
			ctrl.search();
		}

		function rowSelectionChanged(eventRow, selectedRows) {
			ctrl.selectedRows = selectedRows;
		}

		function sortChanged(sortColumns) {
			ctrl.searchCriteria.sortDirection = sortColumns[0] ? sortColumns[0].direction : '';
			ctrl.searchCriteria.sortBy = sortColumns[0] ? sortColumns[0].sortBy : '';
			ctrl.search();
		}
	}
})(angular);
